export function hasClass(el, className) {
  return el.classList.contains(className)
}

export function addClass(el, className) {
  el.classList.add(className)
}

export function getData(el, name, val) {
  const prefix = 'data-'
  if (val) {
    return el.setAttribute(prefix + name, val)
  }
  return el.getAttribute(prefix + name)
}

let elementStyle = document.createElement('div').style

let vendor = (() => {
  let transformNames = {
    webkit: 'webkitTransform',
    Moz: 'MozTransform',
    O: 'OTransform',
    ms: 'msTransform',
    standard: 'transform',
  }

  // eslint-disable-next-line no-unused-vars
  for (let key in transformNames) {
    if (elementStyle[transformNames[key]] !== undefined) {
      return key
    }
  }

  return false
})()

export function prefixStyle(style) {
  if (vendor === false) {
    return false
  }

  if (vendor === 'standard') {
    return style
  }

  return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}

export function hasParent(dom, parentDom) {
  parentDom = Array.isArray(parentDom) ? parentDom : [parentDom]
  while (dom) {
    if (parentDom.find((p) => p === dom)) {
      return true
    } else {
      dom = dom.parentNode
    }
  }
}

export function scrollInto(dom) {
  dom.scrollIntoView({ behavior: 'smooth' })
}

export function isInVisibleArea(dom) {
  if (!dom || !dom.getBoundingClientRect) return false

  const rect = dom.getBoundingClientRect()

  if (rect.top < window.innerHeight && rect.bottom > 0 && rect.left < window.innerWidth && rect.right > 0) {
    return true
  } else {
    return false
  }
}

export const EMPTY_IMG = ''
